package com.qingke.pms.command.user;

import com.qingke.pms.command.UserCommand;
import com.qingke.pms.pojo.Material;
import com.qingke.pms.pojo.User;
import com.qingke.pms.util.PMSConsole;

public class AlterMaterialCommand extends UserCommand {

	public AlterMaterialCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		Material material=null;
		while(true){
			String str=PMSConsole.promptNotEmpty("choose select material by id or code (id,code):");
			if("id".equalsIgnoreCase(str))
				material=byId();
			else if("code".equalsIgnoreCase(str))
				material=byCode();
			else{
				PMSConsole.println("err input");
				continue;
			}
			if(material==null)
				return ;
			break;
		}
		while(true){
			PMSConsole.println("alter (1.code 2.name 3.model 4.unit) 5.confirm alter ");
			String str=PMSConsole.promptNotEmpty("alter material>");
			if("1".equalsIgnoreCase(str))
				code(material);
			else if("2".equalsIgnoreCase(str))
				name(material);
			else if("3".equalsIgnoreCase(str))
				model(material);
			else if("4".equalsIgnoreCase(str))
				unit(material);
			else if("5".equalsIgnoreCase(str))
				break;
			else 
				PMSConsole.println("err input");
		}
		
		boolean isSuccess=dao.updateMaterial(material);
		if(isSuccess)
			PMSConsole.println("update material success.");
		else
			PMSConsole.println("update material failed.");
	}
	
	private Material byId(){
		int id=PMSConsole.getInt("material id:");
		if(!dao.isMaterialExistById(id)){
			PMSConsole.println("the material is not existed.");
			return null;
		}
		return dao.selectMaterialById(id);
	}
	
	private Material byCode(){
		String code=PMSConsole.promptNotEmpty("material code:");
		if(!dao.isMaterialExistByCode(code)){
			PMSConsole.println("the material is not existed.");
			return null;
		}
		return dao.selectMaterialByCode(code);
	}
	
	private void code(Material material){
		String code=PMSConsole.promptNotEmpty("material code:");
		if(dao.isMaterialExistByCode(code)){
			PMSConsole.println("the material code have been existed.");
			return ;
		}
		material.setCode(code);
	}
	
	private void name(Material material){
		String name=PMSConsole.promptNotEmpty("material name:");
		material.setName(name);
	}
	
	private void model(Material material){
		String model=PMSConsole.promptNotEmpty("material model:");
		material.setModel(model);
	}
	
	private void unit(Material material){
		String unit=PMSConsole.promptNotEmpty("material unit:");
		material.setUnit(unit);
	}

}
